import datetime  # 时间计算模块
import time
print(datetime.datetime.now())  # 2018-12-10 16:37:45.939777
print(datetime.date.fromtimestamp(time.time()))  # 时间戳直接转成日期格式 2018-12-10
# 时间加减
print(datetime.datetime.now() + datetime.timedelta(days=3))  # 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(hours=-3))  # 三个小时之前 2018-12-10 13:48:30.000615
print(datetime.datetime.now() + datetime.timedelta(weeks=-5))  # 5周之前
print(datetime.datetime.now() + datetime.timedelta(minutes=30))  # 30分钟之后
# 时间替换
c_time = datetime.datetime.now()
print(c_time)  # 2018-12-10 16:54:35.314510
# 时间替换,指定时间值 2018-12-02 02:03:35.314510,不能为负数
print(c_time.replace(day=2, minute=3, hour=2))